//
//  ZCScroll.m
//  WXWeibo
//
//  Created by apple on 16/5/14.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "ZCScroll.h"

@interface ZCScroll() <UIScrollViewDelegate>

@property (strong, nonatomic) UIScrollView *scroll;
@property (strong, nonatomic) UIImageView *imageView1;
@property (strong, nonatomic) UIImageView *imageView2;
@property (strong, nonatomic) UIImageView *imageView3;
@property (strong, nonatomic) UIPageControl *pages;
@property (strong, nonatomic) NSTimer *timer;

@end

@implementation ZCScroll

- (instancetype)initWithFrame:(CGRect)frame {
    
    if (self = [super initWithFrame:frame]) {
        
        
        [self _createView:frame];
        _imgArr = [NSArray array];
        
        _count = 0;
    }
    return self;
    
}

- (instancetype)initWithFrame:(CGRect)frame withImageArray:(NSArray *)imgArr {
    
    if (self = [super initWithFrame:frame]) {
        
        _imgArr = [NSArray array];
        _imgArr = imgArr;
        [self _createView:frame];
        
        _count = 0;
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self _createView:self.frame];
    }
    return self;
}

- (NSArray *)imgArr {
    
    if (_imgArr == nil) {
        _imgArr = [NSArray array];
        
    }
    return _imgArr;
}


//滑动视图的创建
-(void)_createView:(CGRect)frame {
    
    _scroll = [[UIScrollView alloc] initWithFrame:self.bounds];
    _scroll.contentSize = CGSizeMake(self.frame.size.width * 3, 0);
    _scroll.scrollEnabled = YES;
    _scroll.pagingEnabled = YES;
    _scroll.showsHorizontalScrollIndicator = NO;

    _scroll.backgroundColor = [UIColor clearColor];
    _scroll.contentOffset = CGPointMake(self.frame.size.width, 0);
    _scroll.delegate = self;
    [self addSubview:_scroll];
    
    _imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    [_imageView1 sd_setImageWithURL:[NSURL URLWithString:_imgArr[_imgArr.count -1]]];
    [_scroll addSubview:_imageView1];
    
    _imageView2= [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width, 0, self.frame.size.width, self.frame.size.height)];
    [_imageView2 sd_setImageWithURL:[NSURL URLWithString:_imgArr[0]]];
    
    
    
    [_scroll addSubview:_imageView2];
    
    _imageView3 = [[UIImageView alloc] initWithFrame:CGRectMake(self.frame.size.width * 2, 0, self.frame.size.width, self.frame.size.height)];
    [_imageView3 sd_setImageWithURL:[NSURL URLWithString:_imgArr[1]]];
    
    [_scroll addSubview:_imageView3];
    
    _pages = [[UIPageControl alloc] initWithFrame:CGRectMake(self.frame.size.width - 20 * (_imgArr.count - 1), self.frame.size.height - 20, 20 * _imgArr.count, 20)];
    _pages.currentPage = _count ;
    _pages.numberOfPages = _imgArr.count;
    _pages.pageIndicatorTintColor = [UIColor whiteColor];
    _pages.currentPageIndicatorTintColor = [UIColor colorWithRed:254 / 255.0 green:114 / 255.0 blue:150 / 255.0 alpha:1];
    
    [self insertSubview:_pages aboveSubview:_scroll];
    
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
}



- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    //滑动视图切换始终处于第二张
    if ((scrollView.contentOffset.x) / kScreenWidth > 1.995) {
        
        scrollView.contentOffset = CGPointMake(kScreenWidth , 0);
        _count++;
        if (_count > _imgArr.count - 1) {
            _count = 0;
        }
        [self exchangeData];
        
    }else if((scrollView.contentOffset.x / kScreenWidth) == 0){
        
        scrollView.contentOffset = CGPointMake(kScreenWidth, 0);
        _count--;
        if (_count < 0) {
            _count = _imgArr.count - 1;
        }
        [self exchangeData];
    }
    _pages.currentPage = _count ;
    
}

- (void)exchangeData {
    //界面图片变换
    [_timer invalidate];
    if (_count == 0) {
        
        [_imageView1 sd_setImageWithURL:[NSURL URLWithString:_imgArr[_imgArr.count - 1]]];
        [_imageView2 sd_setImageWithURL:[NSURL URLWithString:_imgArr[0]]];
        [_imageView3 sd_setImageWithURL:[NSURL URLWithString:_imgArr[1]]];
        
    }else if(_count > 0 && _count < _imgArr.count - 1 ){
        
        [_imageView1 sd_setImageWithURL:[NSURL URLWithString:_imgArr[_count -1]]];
        [_imageView2 sd_setImageWithURL:[NSURL URLWithString:_imgArr[_count]]];
        [_imageView3 sd_setImageWithURL:[NSURL URLWithString:_imgArr[_count +1]]];
        
    }else if (_count == _imgArr.count - 1) {
        
        [_imageView1 sd_setImageWithURL:[NSURL URLWithString:_imgArr[_imgArr.count - 2]]];
        [_imageView2 sd_setImageWithURL:[NSURL URLWithString:_imgArr[_imgArr.count - 1]]];
        [_imageView3 sd_setImageWithURL:[NSURL URLWithString:_imgArr[0]]];
        
    }
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(autoScroll) userInfo:nil repeats:YES];
    
}

- (void)autoScroll {
    

    [self exchangeData];
    [self.scroll setContentOffset:CGPointMake(kScreenWidth * 2, 0) animated:YES];
    
}


@end
